package com.md.youyouhui.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.AdapterInformationBinding;
import com.md.youyouhui.net.response.HomeNewsResponse;
import com.md.youyouhui.ui.home.WebNewsActivity;
import com.md.youyouhui.utils.GlideApp;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/4/8.
 */
public class InformationAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterInformationBinding>> {
    private Context context;
    private List<HomeNewsResponse.Rows> rows;

    public InformationAdapter(Context context) {
        this.context = context;
    }

    public void upDate(List<HomeNewsResponse.Rows> rows) {
        this.rows = rows;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public DataBoundViewHolder<AdapterInformationBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        AdapterInformationBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.adapter_information, viewGroup, false);
        return new DataBoundViewHolder<>(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterInformationBinding> holder, int i) {
        holder.binding.infoContent.setText(rows.get(i).describe);
        holder.binding.infoTitle.setText(rows.get(i).title);
        GlideApp.with(context).load(rows.get(i).cover).centerCrop().into(holder.binding.infoImage);
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, WebNewsActivity.class);
            intent.putExtra("title", "新闻详情");
            intent.putExtra("url", rows.get(i).url);
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return null == rows ? 0 : rows.size();
    }
}
